require.config({
    baseUrl: "./modules-list",
    paths: {
        jq: "../js/jquery.2.2.4",
        getJson: "../punlic1/getJson",
        render: "render",
        setLocal: "setLocal"
    }
})
require(["jq", "getJson", "render", "setLocal"], function (_, gj, r, sl) {
    console.log(gj)
    // 1. 先请求数据
    gj({
        url: "./data/goods.json"
    }).then((res) => {
        // console.log(res)
        // 2. 渲染页面
        r({
            data: JSON.parse(res),
            cont: $(".white")
        })
    })
    // 3. 绑定事件
    $(".white").on("click", ".add", function () {
        console.log($(this).parent().attr("index"))
        let id = $(this).parent().attr("index");
        let price = $(this).prev("span").html() - 0;
        // 4. 存数据
        // console.log(id)
        // 要将：id,num,flag,price，存储到localStorage
        sl({ id, price, num: 1, flag: 0 });
    })

    $(".white").on("click", ".productImg", function () {
        let src = this.src;
        let price = $(this).attr("prc")
        console.log(src, price)
        window.open(`./search.html`, "_self")

    })
})
let all = document.querySelector('.all')
let allf = document.querySelector('.all-f')
let sc = document.querySelector('.sc')
let sale = document.querySelector('.sale')
let ulDt = document.querySelectorAll('.fore1')
let unfoldO = document.querySelectorAll('.unfold')

all.onmouseenter = function () {
    allf.style.display = 'block'
}
all.onmouseleave = function () {
    allf.style.display = 'none'
}
sc.addEventListener("mouseenter", fn)
function fn() {
    sale.style.display = "block"
}
sc.addEventListener("mouseleave", fn1)
function fn1() {
    sale.style.display = "none"
}

for (var i = 0; i < ulDt.length; i++) {
    ulDt[i].setAttribute('index', i)
    ulDt[i].onmouseenter = function (e) {
        let eve = e || window.event
        let that = eve.target || eve.srcElement || eve.toElement;
        let index = that.getAttribute("index")
        console.log(index)
        for (var j = 0; j < unfoldO.length; j++) {
            if (j === Number(index)) {
                unfoldO[j].style.display = "block";
            } else {
                unfoldO[j].style.display = "none";
            }
        }
    }
}

const token = sessionStorage.getItem("isLogin") ? JSON.parse(sessionStorage.getItem("isLogin")) : {};
if (token.code === "OK") {
    $(".login").hide();
    $(".user").show();
    $(".user").children("span").html(token.name)
} else {
    $(".login").show();
    $(".user").hide();
}
$(".q").click(function () {
    sessionStorage.removeItem("isLogin");
    $(".login").show();
    $(".user").hide();
})